package pers.mine.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.mine.dao.GoodsItemMapper;
import pers.mine.dao.QueryMapper;
import pers.mine.dto.QueryHandler;
import pers.mine.po.GoodsGroup;
import pers.mine.po.GoodsItem;
import pers.mine.po.GoodsItemExample;
import pers.mine.service.inter.GoodsItemService;
import pers.mine.vo.PageInfo;

/**
 * @author Mine
 * @since 2016年10月20日_下午9:26:55
 */
@Service
public class GoodsItemServiceImpl implements GoodsItemService {
	@Autowired
	private GoodsItemMapper gim;
	@Autowired
	private QueryMapper qm;
	// 日志记录器
	private static final Log logger = LogFactory
			.getLog(GoodsItemServiceImpl.class);

	@Override
	public boolean addGoods(GoodsItem gi) {
		if (gi == null) {
			return false;
		}
		// 设置id 库存 为null
		gi.setId(null);
		gi.setNum(null);

		if (gim.selectByName(gi.getName()) == null) {// 验证商品名
			try {
				// 使用动态插入语句
				if (gim.insertSelective(gi) == 1) {
					return true;
				}
			} catch (Exception e) {
				System.out.println("插入发生异常>>>");
				e.printStackTrace();
			}

		}

		return false;
	}

	@Override
	public boolean editGoodsInfo(GoodsItem gi) {
		if (gi == null || "".equals(gi.getName())) {
			return false;
		}
		// 无须修改图片
		gi.setImg(null);
		// 使用动态更新语句
		if (gim.updateByPrimaryKeySelective(gi) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean editGoodsIMG(Integer gid, String img) {
		// 简单验证合法性
		if (gid == null || img == null || "".equals(img.trim()) || gid <= 0) {
			return false;
		}
		GoodsItem one = new GoodsItem();
		one.setId(gid);
		one.setImg(img);
		// 使用动态更新语句
		if (gim.updateByPrimaryKeySelective(one) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean setGoodsInventory(Integer gid, Integer num) {
		// 简单验证合法性
		if (gid == null || num == null || num < 0 || gid <= 0) {
			return false;
		}
		GoodsItem one = new GoodsItem();
		one.setId(gid);
		one.setNum(num);
		// 使用动态更新语句
		if (gim.updateByPrimaryKeySelective(one) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean setGoodsInventoryByDifference(Integer gid,
			Integer differenceNum) {
		// 简单验证合法性
		if (gid == null || differenceNum == null || differenceNum < 1
				|| gid <= 0) {
			return false;
		}
		GoodsItem one = new GoodsItem();
		one.setId(gid);
		one.setNum(differenceNum);
		// 使用根据差值更新库存语句
		if (gim.updateNumByDifference(one) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean setGoodsShelveState(Integer gid, Integer state) {
		// 简单验证合法性
		if (gid == null || gid <= 0) {
			return false;
		}
		GoodsItem one = new GoodsItem();
		one.setId(gid);
		one.setState(state);
		// 使用动态更新语句
		if (gim.updateByPrimaryKeySelective(one) == 1) {
			return true;
		}
		return false;
	}

	@Override
	public GoodsItem getOneGoodsItemByID(Integer gid) {
		// 简单验证合法性
		if (gid == null || gid <= 0) {
			return null;
		}
		return gim.selectByPrimaryKey(gid);
	}

	@Override
	public PageInfo<GoodsItem> findPageByExample(GoodsItemExample example) {

		List<GoodsItem> ls = gim.selectByExample(example);
		long allSum = gim.getFindRows();

		Integer nowPage = null;
		Integer pageSize = null;
		if (example != null) {
			nowPage = example.getNowPage();
			pageSize = example.getPageSize();
		}
		// 考虑到显示全部查询记录,不分页
		if (nowPage == null) {
			nowPage = 1;
		}
		if (pageSize == null) {
			pageSize = (int) allSum;
		}

		// 这个构造函数可能存在问题...
		PageInfo<GoodsItem> pi = new PageInfo<GoodsItem>((int) allSum, nowPage,
				pageSize);
		pi.setBasePageList(ls);

		return pi;
	}

	@Override
	public PageInfo<GoodsItem> findPageByExampleForIndex(
			GoodsItemExample example) {
		// 只有这不一样..
		List<GoodsItem> ls = gim.selectByExampleForIndex(example);

		long allSum = gim.getFindRows();

		Integer nowPage = null;
		Integer pageSize = null;
		if (example != null) {
			nowPage = example.getNowPage();
			pageSize = example.getPageSize();
		}
		// 考虑到显示全部查询记录,不分页
		if (nowPage == null) {
			nowPage = 1;
		}
		if (pageSize == null) {
			pageSize = (int) allSum;
		}

		// 这个构造函数可能存在问题...
		PageInfo<GoodsItem> pi = new PageInfo<GoodsItem>((int) allSum, nowPage,
				pageSize);
		pi.setBasePageList(ls);

		return pi;
	}

	@Override
	public boolean delGoods(Integer gid) {
		// TODO 自动生成的方法存根
		return false;
	}

	@Override
	public PageInfo<GoodsItem> findPageByQueryHandler(QueryHandler qh) {
		List<GoodsItem> list = qm.queryGoodsItem(qh);
		return getPage(qh, list);
	}

	@Override
	public PageInfo<GoodsItem> findPageOrderBySUM(QueryHandler qh) {
		// 销量
		List<GoodsItem> list = qm.queryGoodsItemOrderBySUM(qh);
		return getPage(qh, list);
	}

	@Override
	public PageInfo<GoodsItem> findPageOrderByGrade(QueryHandler qh) {
		// 评价
		List<GoodsItem> list = qm.queryGoodsItemOrderByGrade(qh);
		return getPage(qh, list);
	}

	@Override
	public PageInfo<GoodsItem> findPageOrderBySelveDate(QueryHandler qh) {
		// 最新上架
		List<GoodsItem> list = qm.queryGoodsItemOrderBySelveDate(qh);
		return getPage(qh, list);
	}

	@Override
	public PageInfo<GoodsItem> findPageByGroupId(Integer groupId,
			QueryHandler qh) {
		// 按组id查商品
		if (groupId == null) {
			if (logger.isErrorEnabled()) {
				logger.error("错误的参数");
			}
			return null;
		}
		if (qh == null) {
			qh = QueryHandler.creat();
		}
		qh.setNeedWhereWord(false); // 不要Where关键字
		qh.put("gid", groupId);
		List<GoodsItem> list = qm.findIndexGoodsItemsByGroupId(qh);
		return getPage(qh, list);
	}

	@Override
	public PageInfo<GoodsItem> findPageWithNoGroupShip(QueryHandler qh) {
		// 未分组商品
		qh.setNeedWhereWord(false);// 不需要Where关键字
		List<GoodsItem> list = qm.queryGoodsItemWithNotGroupShip(qh);
		return getPage(qh, list);
	}

	// 封装page
	private PageInfo<GoodsItem> getPage(QueryHandler qh, List<GoodsItem> list) {
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = 5;
		if (qh == null || qh.getLimitSentence() == null) {// 没分页
			if (logger.isDebugEnabled()) {
				logger.debug("构造PageInfo:未指定分页信息...");
			}
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = qh.getPageNumber();
			pageSize = qh.getPageSize();
			// 获取总记录数
			rootSize = qm.findLastRows();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("构造PageInfo:rootSize=" + rootSize + ",nowPage="
					+ nowPage + ",pageSize=" + pageSize);
		}
		PageInfo<GoodsItem> pi = new PageInfo<GoodsItem>((int) rootSize,
				nowPage, pageSize);
		pi.setBasePageList(list);
		return pi;
	}

	@Override
	public Integer findSaleByID(Integer gid) {
		return qm.getSUMForGoodsItemById(gid);
	}

	@Override
	public Double findGradeByID(Integer gid) {
		return qm.getGradeForGoodsItemById(gid);
	}

}
